package salvo.jesus.graph;

/**
 * Represents an undirected edge in a graph.
 *
 * @author		Jesus M. Salvo Jr.
 */
public class EdgeImpl implements Edge {
  /**
   * The A vertex of the edge.
   */
  protected Vertex  vertexA;

  /**
   * The B vertex of the edge.
   */
  protected Vertex  vertexB;

  /**
   * The Object encapsulated by the Edge
   */
  protected Object  object;

  /**
    * Creates an UndirectedEdge object.
    *
    * @see		Vertex
    */
  public EdgeImpl( Vertex a, Vertex b ) {
    this.vertexA = a;
    this.vertexB = b;
  }

  /**
   * Returns the endpoint A of the edge.
   *
   * @return  Vertex  Endpoint A of the edge.
   */
  public Vertex getVertexA() {
    return this.vertexA;
  }

  /**
   * Returns the endpoint B of the edge.
   *
   * @return  Vertex  Endpoint B of the edge.
   */
  public Vertex getVertexB() {
    return this.vertexB;
  }

  /**
   * Returns the Vertex opposite to the specified Vertex in the edge.
   *
   * @return  Vertex  The Vertex object that is the opposite to the specifid
   *                  Vertex. If the specified Vertex is not an endpoint of the
   *                  edge, returns null.
   */
  public Vertex getOppositeVertex( Vertex v ) {
    if( this.vertexA == v )
      return this.vertexB;
    else if( this.vertexB == v )
      return this.vertexA;
    else
      return null;
  }

  /**
   * Sets an Object that is encapsulated by the Edge
   */
  public void setObject( Object obj ) {
    this.object = obj;
  }

  /**
   * Returns the object encapsulated by the Edge
   */
  public Object getObject() {
    return this.object;
  }

  /**
    * Returns a String representation of the Edge, using the
    * the toString() methods of Vertex. The format is:
    * fromVertex.toString() + "->" + toVertex.toString()
    *
    * @return	The String representation of the Edge
    * @see		Vertex
    */
  public String toString(){
    return vertexA.toString() + "-" + vertexB.toString();
  }

  /**
    * Creates a clone of this Edge. This calls the Edge constructor,
    * thereby creating a new instance of Edge. However, the vertices
    * in both endpoints of the Edge are not cloned.
    *
    * @return  A clone of an instance of Edge.
    */
  protected Object clone(){
    return new EdgeImpl( vertexA, vertexB );
  }

}

